/**
 * Created by wry on 2015/2/12.
 */
'use strict';

angular.module('backofficeApp.replyAtCsService.controllers', []).
   controller('replyAtCsCtrl', ['$scope', '$http', '$location', 'CommunityService', function ($scope, $http, $location, communityService) {

        $scope.communityList = communityService.getAllCommunityService();

        $scope.atStatus = [{
            value:null,
            name:"--请选择--"
        },{
            value:true,
            name:"未处理"
        },{
            value:false,
            name:"已处理"
        }];

        $scope.filter_status = true;

        $scope.currentPage = 1;
        $scope.totalPage = 0;
        $scope.pages = [];

        $scope.getAtList = function () {

            var params = {
                communityId:$scope.filter_communityId,
                status : $scope.filter_status,
                currentPage :$scope.currentPage
            };

            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/clientService/getAtCsList', params, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                $scope.resultList = data.data.list;
                $scope.totalPage = data.data.totalPage;
            }).error(function () {
                $scope.voucherList = null;
            });
        };

        $scope.update = function (id) {
            var params = {
                id:id
            };

            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/clientService/updateAtCsUpdate', params, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                $scope.getAtList();
            }).error(function () {
            });
        };

        $scope.getAtList();

        $scope.detail = function (contentId,category){
            $("#detail_box").css("display","block");
            $("#mask_box").css("display","block");
            $("#iframe_box").attr("src","../../partials/cs/atcs_detail.html?contentId="+contentId+"&category="+category);
            //location.href = "../../partials/cs/atcs_detail.html?contentId="+contentId+"&category="+category;
        }

    /*  宝宝树66小区二次改造    add   gct  20170822   start   */
        $scope.replyMessage = function (id){
            $("#replyMessage_box").css("display","block");
            $("#replyMessageMask_box").css("display","block");
            $("#replyMessageIframe_box").attr("src","../../partials/cs/atcs_replyMessage.html?id="+id);
            //location.href = "../../partials/cs/atcs_detail.html?contentId="+contentId+"&category="+category;
        }
    /*  宝宝树66小区二次改造    add   gct  20170822   end   */

        $scope.onSelectPage = function (page) {
            $scope.currentPage = page;
            $scope.getAtList();
        }

        $("#box_close").on("click",function(){
            $("#detail_box").css("display","none");
            $("#mask_box").css("display","none");
            /*  宝宝树66小区二次改造    add   gct  20170822   start   */
            $("#replyMessage_box").css("display","none");
            /*  宝宝树66小区二次改造    add   gct  20170822   end   */
        });

    }]).directive('mutau', function () {
        return {
            restrict: 'E',
            template: '',
            replace: true,
            link: function (scope, element, attrs) {
                scope.$watch('currentPage', function (value) {
                    scope.pages = [];
                    for (var i = value - 5 > 0 ? value - 5 : 1; i <= (value + 5 > scope.totalPage ? scope.totalPage : value + 5); i++) {
                        scope.pages.push(i);
                    }
                });
                scope.$watch('totalPage', function (value) {
                    scope.pages = [];
                    for (var i = scope.currentPage - 5 > 0 ? scope.currentPage - 5 : 1; i <= (scope.currentPage + 5 > value ? value : scope.currentPage + 5); i++) {
                        scope.pages.push(i);
                    }
                });

                scope.isActive = function (page) {
                    return scope.currentPage === page;
                };
                scope.selectPage = function (page) {
                    if (!scope.isActive(page)) {
                        scope.currentPage = page;
                        scope.onSelectPage(page);
                    }
                };
                scope.selectPrevious = function () {
                    if (!scope.noPrevious()) {
                        scope.selectPage(scope.currentPage - 1);
                    }
                };
                scope.selectNext = function () {
                    if (!scope.noNext()) {
                        scope.selectPage(scope.currentPage + 1);
                    }
                };
                scope.noPrevious = function () {
                    return scope.currentPage == 1;
                };
                scope.noNext = function () {
                    return scope.currentPage == scope.totalPage;
                };
            }
        };
    });

